﻿using UnityEngine;
using System.Collections;
using System;

/// <summary>
/// 玩家，处理移动方法
/// </summary>
public class Player : MonoBehaviour {

    public MeshRenderer[] meshs;
    float moveSpeed = 10f;
    float rotSpeed = 5;

    public UserData userData;
    CharacterController controller;

    void Start()
    {
        controller = this.GetComponent<CharacterController>();
    }

    //设置颜色
    public void SetColor(Color color)
    {
        foreach (var item in meshs)
        {
            item.material.color = color;
        }
    }

    //同步人物的位置
    public void Reset(Vector3 position,Vector3 rotation)
    {
        transform.position = position;
        transform.eulerAngles = rotation;
    }


    Vector2 lastDirect = Vector2.zero;

    //让物体移动
    public void Move(Vector2 direct)
    {
        lastDirect = direct;
    }


    Vector3 move = Vector3.zero;
    Vector3 rota = Vector3.zero;
    private void FixedUpdate()
    {
        if(lastDirect != Vector2.zero)
        {
            //人物的位移
            move.Set(0,0, lastDirect.y);
            move = transform.TransformDirection(move);
            controller.SimpleMove(move * moveSpeed);

            //人物的旋转
            rota.Set(0, lastDirect.x, 0);
            transform.Rotate(rota * rotSpeed);
        }
    }
}


